package com.example.baseweather;

import android.Manifest;
import android.app.ActivityOptions;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Rect;
import android.hardware.display.DisplayManager;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.baseweather.databinding.ActivityMainBinding;
import com.example.baseweather.databinding.PopWindowViewBinding;
import com.example.baseweather.domain.VideoInfoBean;
import com.example.baseweather.ui.VideoPlayActivity;
import com.example.baseweather.ui.adapter.VideoRecyclerViewAdapter;
import com.example.baseweather.utils.CommonUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    public static HashMap<String,Integer> map = new HashMap<>();


    private static final String[] permissions = {
            Manifest.permission.READ_EXTERNAL_STORAGE
    };

    private static final int REQUEST_CODE = 0x0001;
    private ActivityMainBinding binding;

    private DisplayManager display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        display = ((DisplayManager) this.getSystemService(Context.DISPLAY_SERVICE));
        requestPe();
        initData();
    }


    private void initData() {
        File[] videoFiles = CommonUtils.getVideoPath();
        if (videoFiles.length == 0) {
            CommonUtils.showToastShort("当前无视频", this);
        }
        List<VideoInfoBean> videoInfoBeans = new ArrayList<>();
        for (File videoFile : videoFiles) {
            VideoInfoBean videoInfo = CommonUtils.getVideoInfo(videoFile);
            videoInfoBeans.add(videoInfo);
        }
        initRecyclerView(videoInfoBeans);
    }

    private void initRecyclerView(List<VideoInfoBean> videoInfoBeans) {
        binding.rvVideoInfo.setLayoutManager(new GridLayoutManager(this, 4));
        binding.rvVideoInfo.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                int i = CommonUtils.dpToPx(view.getContext(), 8);
                outRect.left = i;
                outRect.right = i;
                outRect.top = i;
                outRect.bottom = i;
            }
        });
        VideoRecyclerViewAdapter adapter = new VideoRecyclerViewAdapter(videoInfoBeans);
        adapter.setListener((view, position) -> {
            PopupWindow popUpWindow = createPopUpWindow();
            popUpWindow.showAsDropDown(view);
            map.put(getPackageName()+"video",position);
        });
        binding.rvVideoInfo.setAdapter(adapter);
    }


    private PopupWindow createPopUpWindow() {
        PopWindowViewBinding binding = PopWindowViewBinding.inflate(getLayoutInflater());
        PopupWindow popupWindow = new PopupWindow(
                binding.getRoot(),
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                true); // true 表示这个窗口可以获取焦点，这样可以处理点击外部区域自动关闭窗口的行为
        binding.tvSecond.setOnClickListener(v -> {
            displaySecondScreen();
        });
        binding.tvBoth.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, VideoPlayActivity.class);
            startActivity(intent);
            displaySecondScreen();
        });
        return popupWindow;
    }


    private void displaySecondScreen() {
        Intent intent = new Intent(MainActivity.this,VideoPlayActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ActivityOptions options = ActivityOptions.makeBasic();
        options.setLaunchDisplayId(display.getDisplays()[2].getDisplayId());
        startActivity(intent,options.toBundle());
    }


    private void requestPe() {
        ArrayList<String> strings = new ArrayList<>();
        Arrays.stream(permissions).forEach(item -> {
            if (ContextCompat.checkSelfPermission(this, item) != PackageManager.PERMISSION_GRANTED) {
                strings.add(item);
            }
        });
        //全部通过
        if (strings.size() == 0) {
            CommonUtils.showToastShort("全部权限申请已通过", this);
        } else {
            requestPermissions(strings.toArray(new String[0]), REQUEST_CODE);
        }

    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        //请求码同且请求通过
        if (requestCode == REQUEST_CODE && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            CommonUtils.showToastShort("全部权限申请已通过", this);
        }

    }




}